﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BMSQ.Utility;

namespace BMSQ.Admin.Accounts
{
    public partial class UserRole : PageBase
    {
        private string UserID
        {
            get
            {
                if (Request.QueryString["UserID"] == null)
                {
                    return "";
                }
                else
                {
                    return Request.QueryString["UserID"].ToString();
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (UserID == "")
            {
                Response.Write("传入不正确");
                Response.End();
                return;
            }

            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            Vincent.Permission.Model.account_userlist user = Vincent.Permission.Functions.Account.GetSingleUser(UserID); //new BLL.account_userlist().GetModel(UserID);
            if (user == null)
            {
                MessageBox.Show(this.Page, "当前用户不存在");
                return;
            }
            lblUser.Text = string.Format("正在为[{0}]分配角色。", user.UserName);

            List<Vincent.Permission.Model.account_roles> roles = Vincent.Permission.Functions.Account.GetRolesCollection(""); //new BLL.account_roles().GetModelList("");
            if (roles.Count == 0)
            {
                MessageBox.Show(this.Page, "还没有为系统创建角色");
                return;
            }
            cblRolesList.DataSource = roles;
            cblRolesList.DataValueField = "RoleID";
            cblRolesList.DataTextField = "RoleName";
            cblRolesList.DataBind();

            List<Vincent.Permission.Model.account_roles> userRoles = Vincent.Permission.Functions.Account.GetUserRoles(UserID);//  new BLL.account_userroles().GetUserRoles(UserID);
            foreach (ListItem li in cblRolesList.Items)
            {
                if (userRoles.Find(p => p.RoleID == li.Value) == null)
                {
                    li.Selected = false;
                }
                else
                {
                    li.Selected = true;
                }
            }
        }

        private void Save()
        {
            List<Vincent.Permission.Model.account_userroles> lst = new List<Vincent.Permission.Model.account_userroles>();
            foreach (ListItem li in cblRolesList.Items)
            {
                if (li.Selected == true)
                {
                    Vincent.Permission.Model.account_userroles model = new Vincent.Permission.Model.account_userroles() { RoleID = li.Value, UserID = UserID };
                    lst.Add(model);
                }
            }

            Vincent.Permission.Functions.Account.DeleteUserRoles(UserID);
            Vincent.Permission.Functions.Account.SetUserRoles(lst);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Save();
            MessageBox.Show(this.Page, "保存成功");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("User/list.aspx");
        }
    }
}